package com.tencent.sr.iris.basic.saas.common.utils;

import com.tencent.sr.iris.basic.saas.common.exception.UserEssErrorCode;
import com.tencent.sr.rmall.springbootstarter.authtoken.localbean.AppUser;
import com.tencent.sr.rmall.springbootstarter.authtoken.localbean.ManageSession;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;

/**
 * 业务工具类
 */
@Slf4j
public class BizUtil {

    private static final String VALID = "有效";
    private static final String INVALID = "无效";

    /**
     * 获取rootSaasId
     *
     * @param appUser
     * @return rootSaasId
     */
    public static Long getRootSaasId(AppUser appUser) {
        ManageSession manageSession = appUser.getManageSession();
        CheckerUtil.checkNotNull(manageSession, UserEssErrorCode.NO_USER_INFO);
        Long rootSaasId = manageSession.getRootSaaSId();
        CheckerUtil.checkNotNull(rootSaasId, UserEssErrorCode.NO_USER_INFO);
        return rootSaasId;
    }

    /**
     * 获取uid
     *
     * @param appUser
     * @return uid
     */
    public static String getUid(AppUser appUser) {
        String uid = appUser.getUid();
        CheckerUtil.checkNotNull(uid, UserEssErrorCode.NO_USER_INFO);
        return uid;
    }

    /**
     * 是都有效转换 0->无效 1->有效
     *
     * @param valid
     * @return
     */
    public static String getIsValid(Integer valid) {
        if (Objects.isNull(valid)) {
            return "";
        }
        if (valid.equals(0)) {
            return INVALID;
        }
        if (valid.equals(1)) {
            return VALID;
        }
        return "";
    }
}